package com.lancoo.ccas52.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lancoo.ccas52.entity.Room;
import com.lancoo.ccas52.entity.Student;
import com.lancoo.ccas52.entity.Teacher;
import com.lancoo.ccas52.entity.TeachingClass;
import com.lancoo.ccas52.mapper.RoomMapper;
import com.lancoo.ccas52.mapper.TeacherMapper;
import com.lancoo.ccas52.mapper.TeachingClassTeacherMapper;
import com.lancoo.ccas52.pojo.common.CollComparatorUtil;
import com.lancoo.ccas52.pojo.common.PageInfo;
import com.lancoo.ccas52.pojo.dto.TeachingClassDto;
import com.lancoo.ccas52.pojo.response.basicPlatform.BaseTeacher;
import com.lancoo.ccas52.pojo.vo.Campus;
import com.lancoo.ccas52.pojo.vo.TeacherGroupCollege;
import com.lancoo.ccas52.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas52.service.TeachingClassService;
import com.lancoo.ccas52.util.PagingUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 教师 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2023-01-31
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private TeachingClassTeacherMapper teachingClassTeacherMapper;
    @Resource
    private CollComparatorUtil collComparatorUtil;


    @Override
    public List<Teacher> getTeacherByTeachingClassIds(List<Long> teachingClassIds) {
        if (CollUtil.isEmpty(teachingClassIds)) {
            return new ArrayList<>();
        }
        List<Teacher> teacherList = teachingClassTeacherMapper.getTeacherByTeachingClassIds(teachingClassIds);
        return teacherList;
    }

    @Override
    public List<TeacherGroupCollege> getAllTeacherGroupCollege( String schoolId,String term) {
        List<TeacherGroupCollege> teacherGroupCollegeList = new ArrayList<>();
        List<Teacher> list = this.list(new LambdaQueryWrapper<Teacher>().eq(Teacher::getSchoolId, schoolId).eq(Teacher::getTerm,term).eq(Teacher::getIsEnable, 1));
        //根据学院id 名字分组
        Map<Pair<String, String>, List<Teacher>> listMap = list.stream().collect(Collectors.groupingBy(p -> Pair.of(p.getCollegeId(), p.getCollegeName())));
        listMap.entrySet().forEach(o -> {
            TeacherGroupCollege teacherGroupCollege = new TeacherGroupCollege();
            teacherGroupCollege.setCollegeId(o.getKey().getKey());
            teacherGroupCollege.setCollegeName(o.getKey().getValue());
            teacherGroupCollege.setTeacherList(o.getValue());
            teacherGroupCollegeList.add(teacherGroupCollege);
        });
        return teacherGroupCollegeList;
    }

    @Override
    public Teacher getTeacherByBaseId(String schoolId,String term, String baseTeacherId) {
        Teacher teacher = this.getOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getSchoolId, schoolId).eq(Teacher::getTerm,term).eq(Teacher::getBaseTeacherId, baseTeacherId));
        return teacher;
    }

    @Override
    public void syncBaseTeachers(Integer taskId, List<BaseTeacher> baseTeachers, String schoolId,String term) {
        List<Teacher> syncTeacherList = new ArrayList<>();
        List<Teacher> updateTeacherList = new ArrayList<>();

        for (BaseTeacher baseTeacher : baseTeachers) {
            syncTeacherList.add(baseTeacher.toTeacher(taskId,schoolId,term));
//            Arrays.stream(baseTeacher.getSubjectIDs().split(",")).forEach(s -> {
//                teacherSubjectList.add(TeacherSubject.builder()
//                        .teacherId(baseTeacher.getUserID())
//                        .subjectId(s)
//                        .build());
//            });
        }
        List<Teacher> oldTeacherList = this.lambdaQuery().eq(Teacher::getSchoolId, schoolId).eq(Teacher::getTerm,term).list();
        List<Teacher> addTeacher = collComparatorUtil.getAdded(oldTeacherList, syncTeacherList);
        //教师去重复 更新
        if (CollUtil.isNotEmpty(addTeacher)) {
            teacherMapper.addBatch(addTeacher);
        }
    }

    @Override
    public PageInfo<Teacher> getTeacherList(Integer taskId, String schoolId,String term, IPage<Teacher> page, String collegeId, String keyWord) {
        IPage<Teacher> teacherPage = this.page(page, new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getSchoolId,schoolId).eq(Teacher::getTerm,term)
                .eq(StrUtil.isNotBlank(collegeId), Teacher::getCollegeId, collegeId)
                .like(StrUtil.isNotBlank(keyWord), Teacher::getTeacherName, keyWord)
                .orderByAsc(Teacher::getBaseTeacherId));
        return PagingUtil.formatPageResult(teacherPage);
    }

    @Override
    public List<TeachingClassDto> getTeachingClassByTeacherId(List<Long> teacherIds) {
        List<TeachingClassDto> teachingClassByTeacherId = teacherMapper.getTeachingClassByTeacherId(teacherIds);
        return teachingClassByTeacherId;
    }


}
